#! /bin/bash
## English version
### 	WARNING THIS SCRIPT IS FOR SUPERUSER ONLY and TERMINAL CONSOL    ###

#  NVIDIA-MAKER
#  Sat Feb  6 16:58:20 2010
#  Copyright  2010  mk67
#  <rmk677@gmail.com>

#  This program is free software; you can redistribute it and/or modify
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#  
#  You should have received a copy of the GNU Lesser General Public
#  License along with main.c;if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA

cd /
### VARS
### -------------------------------------------------------
## Master Vars.
nvdir="/home/dvr/Public/NVIDIA"
nvtar="$nvdir/tgz"					# archies directory
nvtmp="$nvdir/temp"					# install temp directory
nvlog="$nvdir/log"					# logs directory
nvdl="$nvdir/release"					# downlaod driver backups directory
nvupdate="$nvdir/update"				# update temp directory
logfile="--log-file-name=$nvlog/install.log"		# nvidia-installer options
temp="--tmpdir=$nvtmp"					# nvidia-installer option: install temp dir
dltemp="--tmpdir=$nvupdate"				# nvidia-installer option: update temp dir

## Master Menu Vars
A=" From a local archive."
B=" From a local package."
C=" From NVIDIA server ( are you connected?)."
D=" Build an archive only."
E=" Uninstall actual driver"
F=" Stop X server."
G=" Restart X server."
H=" Check aviable updates."
I=" Quit." 

## Script colours Vars.
t='\e[0;39m'	#default terminal colour
r='\e[1;31m'	#red
v='\e[1;32m'	#green
y='\e[1;36m'	#cyan
j='\e[1;33m'	#yellow

### VARS
## card id 
board=$(lspci -nn | grep 'VGA' | awk '{ print $9" "$10" "$11" "$12" "$13 }' | tr '[[]]' ' ')
## vendor
vendor=$(lspci -nn | grep 'VGA' | awk '{ print  $1" "$6" "$7 }')
## system arch
ARCH=`uname -a | awk '{ print $12 }'`
if [ $ARCH == "i686" ] || [ $ARCH == "i586" ] ; then
	ARCH="x86"
	RUN_PCK="pkg1"
else
	RUN_PCK="pkg2"
fi
## system distro --> need progress
function distro(){
if [ -f /proc/version ] ; then
	version="cat /proc/version"
	cat /proc/version | grep ubuntu > /dev/null
	if [[ $? -eq 0 ]]; then
	DISTRO="UBUNTU"
	PKG_INSTALLER="apt-get install"
	fi
	cat /proc/version | grep mandriva > /dev/null
	if [[ $? -eq 0 ]]; then
	DISTRO="MANDRIVA"
	PKG_INSTALLER="urpmi"
	fi
else
	echo -e "\f$r Distro is not . Install script dependences manuelly "
fi
}
# dependencies control
function dep_control(){
	ls /usr/bin/lftp > /dev/null
	if [ $? -ne 0 ] ; then 
		echo -e "$v Required dependencies are not installed"; sleep 2
		echo -e "$y INSTALL RUNNING ..."
		PKG="lftp kernel-devel gcc wget"
		$PKG_INSTALLER $PKG
		echo -e "$y INSTALL DONE. "
	fi
}
# Internet connection control function.
function ifnet(){
	for net in question ; do 
		if [ -n /sbin/ifconfig ] ; then
			if [[ "$( /sbin/ifconfig | grep 'ppp0' | awk '{print $1}' )" != "ppp0" ]] ; then
				net="ppp0"
			elif [[ "$( /sbin/ifconfig | grep 'eth0' | awk '{print $1}' )" != "eth0" ]] ; then
				net="eth0"
			elif [[ "$( /sbin/ifconfig | grep 'eth1' | awk '{print $1}' )" != "eth1" ]] ; then
				net="eth1"
			elif [[ "$( /sbin/ifconfig | grep 'wlan0' | awk '{print $1}' )" != "wlan0" ]] ; then
				net="wlan0"
			fi
		fi
		/sbin/ifconfig $net | awk '{print $2}' | sed -n '2p'
	done
}
# Primary control of X and network connection.
function xlocksession(){
	if [ -e /tmp/.X0-lock ] ; then				# TEST >> if [[ `runlevel | awk '{print $2}'` == 5 ]] ; then
		echo -e "\f$y  I'm actually in Graphic Mode. I need to stop it.\n  Once in terminal session, restart this script in ROOT.$t\f"
		echo -e "\f$v  Note:$y If you only wish to check last driver version aviable,\n  it isn't necessary to stop X server.$t\f"
		if [[ `ifnet` == adr:127.* ]] ; then
			echo -e "$r  WARNING!$v You are not connected to the network.\n If I has to make an update from the server, I need a valid connection.\n Restart me after the connection$t\f"
		else
		echo -e "\f$v Do you want me to stop the X session for you?$j (y/n)?\n$v ==> $j\c"
		read -n 1 wilu ; echo -e "$v"
			if [ $wilu == "y" ] ; then
			echo -e "$y \f BEWARE ABOUT YOUR CURRENT WORK, CLOSE ALL APPLICATIONS\r BEFORE THIS COMMAND VALIDATION"
			echo -e "$v\r\\t Type $j[C]$v to confirm.\n\t Else, type$j [Q]$v to leave. "
				read -s -n 1 confirm
				if [ $confirm == Q ] ; then
					exit 1
				else
					if [ $confirm == C ] ; then
					echo -e "\t INIT ...." ; sleep 2
					/usr/sbin/gdm-stop
					fi
				fi
			fi
		#sleep 1 ; exit 1
		#menu
		fi		
	fi
}
# making links and originals libraries
function rmlink(){
	function librmlk(){
		rm -vf $class/libGL.*
		rm -vf $class/libGLcore.*
		rm -vf $class/libcuda.*
		rm -vf $class/libXvMCNVIDIA.*
		rm -vf $class/libnvidia-cfg.*
		rm -vf $class/libnvidia-tls.*
		rm -vf $class/libvdpau.so.*
		rm -vf $class/libvdpau_nvidia.so.*
		rm -vf $class/libvdpau_trace.so.*
	}
	function xorgrmlk(){
		rm -vf $class/xorg/modules/libnvidia-wfb.*
		rm -vf $class/xorg/modules/extensions/libglx.*
	}
	function tlsrmlk(){
		rm -vf $class/tls/libnvidia-tls.*
	}
	lib64="/usr/lib64"
	lib32="/usr/lib"
	if [[ `uname -a | awk '{ print $15 }'` -eq 64 ]] ; then
		#for lib64 in rmlink ; do
		class=$lib64
		librmlk ; xorgrmlk ; tlsrmlk
		#done
		#for lib32 in rmlink ; do
		class=$lib32
		librmlk ; tlsrmlk
		#done
	else
		if [[ `uname -a | awk '{ print $15 }'` != 64 ]] ; then
			class=$lib32
			librmlk ; xorgrmlk ; tlsrmlk
		fi
	fi
}
# Interanl archive links creation
function makelink(){
	function usrlink(){
		ln -s -f -T $class/libGL.so.* $class/libGL.so.1
		ln -s -f -T $class/libGL.so.1 $class/libGL.so
		ln -s -f -T $class/libGLcore.so.* $class/libGLcore.so.1
		ln -s -f -T $class/libcuda.so.* $class/libcuda.so.1
		ln -s -f -T $class/libcuda.so.1 $class/libcuda.so
		ln -s -f -T $class/libnvidia-cfg.so.* $class/libnvidia-cfg.so.1
		ln -s -f -T $class/libnvidia-cfg.so.1 $class/libnvidia-cfg.so
		ln -s -f -T $class/libnvidia-tls.so.* $class/libnvidia-tls.so.1
		ln -s -f -T $class/libvdpau.so.* $class/libvdpau.so.1
		ln -s -f -T $class/libvdpau.so.1 $class/libvdpau.so
		ln -s -f -T $class/libvdpau_nvidia.so.* $class/libvdpau_nvidia.so
		ln -s -f -T $class/libvdpau_trace.so.* $class/libvdpau_trace.so
	}
	function xorglink(){
		ln -s -f -T $class/libXvMCNVIDIA.so.* $class/libXvMCNVIDIA_dynamic.so.1
		ln -s -f -T $class/libXvMCNVIDIA_dynamic.so.1 $class/libXvMCNVIDIA_dynamic.so
		ln -s -f -T $class/xorg/modules/libnvidia-wfb.so.* $class/xorg/modules/libnvidia-wfb.so.1
		ln -s -f -T $class/xorg/modules/extensions/libglx.so.* $class/xorg/modules/extensions/libglx.so
	}
	function tlslink(){
		ln -s -f -T $class/tls/libnvidia-tls.so.* $class/tls/libnvidia-tls.so.1
	}
	lib64="/usr/lib64"
	lib32="/usr/lib"
	if [[ `uname -a | awk '{ print $15 }'` -eq 64 ]] ; then
		class=$lib64
		usrlink ; tlslink ; xorglink ; sleep 1
		class=$lib32
		usrlink ; tlslink ; sleep 1
	else
		if [[ `uname -a | awk '{ print $15 }'` != 64 ]] ; then
			class=$lib32
			usrlink ; tlslink ; xorglink ; sleep 1
		fi
	fi
}
# Directories and files listing.
function list(){
		#listdir="$tmplist"
		wlist=`ls -d $listdir$opt1$opt2$opt3 | sed 's|'$optlist'|--->  |' | cat -n`
		echo "$wlist"
		echo -e "\r$v Your Choice :\n ==>  $j\c"
		read list
		result=`echo "$wlist" | sed -n ''$list'p'`
		#echo ${result#*--> }
}
## Some Other Vars.
# Check Installed version
# elf type
function libclass(){
	if [[ $(uname -a | awk '{ print $15 }') == 64 ]] ; then
		ELF_TYPE="64"
	else
		ELF_TYPE=""
	fi
}
function version_id(){
	libclass
	version=$(echo /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.*)
	echo ${version#*so.}
}
# check aviable updates
function check_update(){
	lftp -c "anon; cd ftp://download.nvidia.com/XFree86/Linux-$ARCH/ ; ls >> $nvtmp/drvlist ; cat latest.txt >> $nvtmp/last_up ; quit"
	LAST_IN=$(version_id)
	LAST_DRV=$(cat $nvtmp/last_up | awk '{ print $1 }')
	LAST_BETA=$(cat $nvtmp/drvlist | awk '{ print $9 }' | sort -gr | sed -n 1p)
	echo -e "\n\t$v Here is avaible update for your $y$board$v card." 
	echo -e "\n\t$v Present version is :\t$j$LAST_IN "
	echo -e "\n\t$v Last official is :\t$j$LAST_DRV "
	echo -e "\n\t$v Last BETA is :\t\t$j$LAST_BETA "
}
### ARCHIVED DRIVERS INSTALL FUNCTION.
function from_archive(){
	echo -e "$v Choose an archive in the list:$y\r"
	cd $nvtar
		listdir="$nvtar"
		opt1="/*"
		optlist="${listdir#*}"
	list
	drivertar=${result#*-->  /}
	echo -e "\n$v You choose$j ${result#*-->  /}."
	echo -e "\n$r --->$y     UNARCHIVING RUNNING!$r     <---$y\r"
	cd /
	echo -e "\n$j Deleting originals files and links...$y"
	rmlink
	cp -T `tar xvf $nvtar/$drivertar` /
	echo -e "\n$j Links creation... $y\n"
	makelink
	libclass
	if [ -f /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.$(version_id) ] ; then
		ls -l /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.$(version_id) | awk '{ print " Links updated at " $6 " à " $7}'
		echo -e "\n$r >>$v Creation ends nicely.$r<<"
	else
		echo -e "\n$v >>$r Creatio ends badly.$v<<"
		echo -e "\n$v Back to master menu. "
		menu
	fi
	echo -e "\n$v I need to restart X server for changes take effect."
	echo -e "$v Will you?\n$j (y/n)$r ==>$j \c"
	read -n 1 dmrestart
		if [ $dmrestart == "y" ] ; then
			echo -e "$v Restarting X server.\r" ; sleep 2
			modprobe -r nvidia ; depmod -qa
			modprobe nvidia ; dm_restart
			#break
		else
			echo -e "$v\n" ; menu
		fi
}

### FROM A USER DIRECTORY INSTALL FUNCTION.
function from_directory(){
	function installdrv(){
		if [[ -s $driverep ]] ; then
			echo -e "\n$r --->$v     INSTALL STARTS, PLEASE WAIT!$r     <---$y\r"
			sh $driverep -a -n -q --ui=none $temp $logfile
			echo -e "$j Congratuaktion! Your driver is installed.\n$y" ; sleep 2
			cd $nvtmp ; sh $driverep -x
			nvlv=`$nvdir/nvidia-installer --latest --ui=none | sed -n '5p' | awk '{ print $4 }'`			
			#drv_actu=`ls -D | grep '${nvlv%;*}'`		# liste les Répertoire NVIDIA présent
			if [ -x $driverep ] ; then
				#cp -f $driverrep/nvidia-installer $nvdir
				cp -f $driverep $nvdl/nv-update-${nvlv%;*}
				if [[ -f $nvdl/nv-update-${nvlv%;*} ]] ; then
					echo -e "\n$v Backup copy of the driver$j ${nvlv%;*}$v done,\n I could proceed to archiving.\n"
					sleep 1
				else
					echo -e "\n$v Driver backup copy $j ${nvlv%;*}$v has ended anormally."
					echo -e "\n$v The Script needs this copy for archiving and many other operations."
					echo -e "\n$v Do it manually or/and contact the author about the bug."
					echo -e "\n$r BIG WARNING$v DON'T DO ANY BACKUP\n before the file copy\n presence in $nvdl directory!"
					echo -e "\n$v Hit a key to continu."
					read -n 1 ok
				fi				
			fi
			archivage
		else
			echo -e "\n$y Ouch!$v You don't have a valid NVIDIA package in this directory \n I suggest you to go to NVIDIA site,\n"
			echo -e "\n here :$y http://www.nvidia.fr/Download/Find.aspx?lang=en$v or to the NVIDIA ftp repos,"
			echo -e "\n here:$y ftp://download.nvidia.com/XFree86/"
			echo -e "\n$v and, please baby come back to me!.$t\n"
			echo -e "\n$v Back to main menu or restart X server ? $j"
			select answer in "Main menu." "Restart X." ; do
				case $answer in
					"Main menu.")
						menu
						;;
					"Resstart X.")
						dm_restart
						;;
				esac
			done
		fi
	}
	echo -e "$v Here is the list of already present driver in NVIDIA directory:$y\n"
	ls $nvdl
	echo -e "\n$v Will you use one of those versions or choose an other from user directory ?:$j\n"
		VP=" Present version"
		RU=" User dir"
	select repo in "$VP" "$RU" ; do
		case $repo in
			"$VP")
				echo -e "$y" ; cd $nvdl
				echo -e "$v Here is $nvdl content: $y"
				listdir="$nvdl"
				opt1="/*"
				opt3=""
				optlist="${listdir#*}"
				list
				driver="${result#*-->  /}" 
				driverep="$nvdl/$driver" ; installdrv
			;;
			"$RU")
				# niveau primaire
				echo -e "$y" ; cd /home
				listdir="/home"
				opt1="/*"
				opt3="/"
				optlist="${listdir#}"
				list
				homerep="${result#*-->  $opt3}" ; echo $homerep
				cd $homerep
				echo -e "\n$v Search continu in$j $homerep$y," ; sleep 1
				ls  -d /home/$homerep/* | sed 's|/home/'${homerep#*}'|--->  |'
				echo -e "\n$v Use [TAB] to follow the yellow brick road to your driver :"
				echo -e "$v ==> $j\c"
				read -e driver
				driverep="/home/$homerep/$driver"
				chmod a+x $driverep
				installdrv
			;;
		esac
	done
}

### UPDATE FUNCTION, FROM INTERNET.
function from_net(){
# download functions 
	function last_pack(){
		echo -e "\n$r --->$v     UPDATE, PLEASE WAIT!$r $y\r"
		wget -c --progress=bar:force ftp://download.nvidia.com/XFree86/Linux-$ARCH/$LAST_PACK/*$RUN_PCK* $nvupdate/
		echo -e "\n$r --->$v     DOWNLOAD DONE.$r $y\r" ; sleep 1
		echo -e "\n$r --->$v     PACKAGE ARCHIVE EXTRATION$r $y\r" ; sleep 1
	}
	function installe(){
		#nvr=`$nvi | sed -n '6,$p'`
		nwdrv=`ls $nvupdate | grep $LAST_PACK` ; echo "$nwdrv"
		sh $nwdrv -a -q -n --ui=none $dltemp $logfile
		# pre achiving
		if [[ $(ls $nvupdate/$nwdrv) ]] ; then
			sh $nvupdate/$nwdrv -x
			cd ../
		else
			echo -e "\n$j Houston! We got a problrm over there, no temp dir!\n Back to main\n "
			sleep 1 ; menu
		fi
		drvrep=`ls -D | grep '$LAST_PACK'`		# NVIDIA directory listing
		cp -f $nvupdate/$drvrep/nvidia-installer $nvdir
		mv -T $nvupdate/$nwdrv $nvdl/nv-update-$LAST_PACK
		mv -f $nvupdate/* $nvtmp/
		chmod 755 $nvtmp/*
	}
# script
if [[ `ifnet` == adr:127.* ]] ; then
	echo -e "\f$v I'm not connected; \n Connect me and restart the script.$t"
	menu
else
	if [ -e $nvdir/nvidia-installer ] ; then
		cd $nvupdate
		# le binaire Nvidia à un peu de mal avec l'exec après transfert,
		# aidons-le un peu.
		check_update
		echo -e "\n\t$v Type $j[C]$v to choose one of those versions.\n\t Else, type $j[M]$v to go back to main menu."
			read -s -n 1 confirm2
			if [ $confirm2 == C ] ; then
				echo -e "$v\n Please choose your version:$j\r"
				A1="$LAST_DRV"
				A2="$LAST_BETA"
				PS3="`echo -e "\n$v Your Choice$j ==>"` "
				select choice4 in "$A1" "$A2" ; do 
					case $choice4 in
						"$A1")
							echo -e "$v--->  Official driver installation begins...  <---\r"
							LAST_PACK=$LAST_DRV
							last_pack
							installe
							rm -f $nvtmp/drvlist $nvtmp/last_up
							archivage
						;;
						"$A2")
							echo -e "$v--->  BETA driver installation begins...  <---\r"
							LAST_PACK=$LAST_BETA
							last_pack
							installe
							rm -f $nvtmp/drvlist $nvtmp/last_up
							archivage
						;;
					esac
				done
			else
				if [ $confirm2 == M ] ; then
					rm -f $nvtmp/drvlist $nvtmp/last_up
					sleep 1 ; menu
				fi
			fi
	else
		echo -e "$j Ooops!$v No nvidia-installer in my dir.\n I have to work with local package "
		sleep 1 ; $B
	fi
fi
}



### ARCHIVING. ###
function drivercall(){
	ls $nvtmp | grep $(version_id)
} 
# sub function: METHOD CHOICE
function archivage(){
echo -e "$v I can bckup the driver now,\n Do you wish?$j (y/n)$v:"
	echo -e "\n$v ==>$j \c" ; read -n 1 archive
	case $archive in
		Y|y|O|o)
			echo -e "\n$v Controling..." ; sleep 1
			echo -e "$y $(ls -d $nvtmp/$(drivercall))"
			cd $nvtmp
			if [ -f $(ls $nvtmp | grep $(version_id))/LICENSE ] ; then
				echo -e "\n$v\"$j $(drivercall) $v\" is present and correctly unzip.\n$y"
			else
				echo -e "\n$v The$r working temp directory$v is empty.\n Something bad in the script ?."
				sleep 1
			fi
			archive ; sleep 1
			echo -e "$v\n  need to restart X server" ; sleep 1
	 		echo -e "$v\n Choose $j$G$v inthe main menu.\n"
			rm -f -R $nvtmp/*
			sleep 2 ; menu
			;;
		N|n)
			rm -f -R $nvtmp/*
			echo -e "$v\n I could build the archive later\n from $j$D$v in main menu." ; sleep 1
			echo -e "$v\n I need to restart DM service now"
	 		echo -e "$v\n Select $j$G$v in main menu.\n"
			sleep 2 ; menu
	 		;;
	esac
}

### REAL ARCHIVING
function archive(){
cd $nvtmp
## archive function
 	function do_tar(){
		if [[ -d $nvtmp/$nvrep ]] ; then
			echo -e "\n$r>>$j Proceed to creation ofthe archive $(version_id). $r <<$y\n" ; sleep 2
		else
			echo -e "\n$r Working temporary directory doesn't exist."
			echo -e "\n$v Check your package or contact the author if it come from the script$y\n" ; sleep 1
			rm -f -R $nvtmp/* ; menu
		fi
		tmpdir=$(version_id)
		mkdir  -v -p $tmpdir
		if [ -d $nvtmp/$tmpdir ] && [ -d $nvtmp/$nvrep ] ; then
				mkdir -p $tmpdir/lib/modules/`uname -r`/kernel/drivers/video
				mkdir -p $tmpdir/usr/lib
				mkdir -p $tmpdir/usr/lib/tls
				mkdir -p $tmpdir/usr/bin
				mkdir -p $tmpdir/usr/include
				mkdir -p $tmpdir/usr/share
				cp /lib/modules/`uname -r`/kernel/drivers/video/nvidia.ko $tmpdir/lib/modules/`uname -r`/kernel/drivers/video/
				cp $nvrep/usr/bin/* $tmpdir/usr/bin/
				cp  -R $nvrep/usr/include/* $tmpdir/usr/include/
				cp  -R $nvrep/usr/share/* $tmpdir/usr/share/
			if [[ `uname -a | awk '{ print $15 }'` == 64 ]] ; then
				mkdir -p $tmpdir/usr/lib64/xorg/modules
				mkdir -p $tmpdir/usr/lib64/tls
				cp $nvrep/usr/X11R6/lib/* $tmpdir/usr/lib64/
				cp  -R $nvrep/usr/X11R6/lib/modules/* $tmpdir/usr/lib64/xorg/modules/
				cp  -R $nvrep/usr/lib/* $tmpdir/usr/lib64/
				cp  -R $nvrep/usr/lib32/* $tmpdir/usr/lib/
			else
				mkdir -p $tmpdir/usr/lib/xorg/modules
				cp  $nvrep/usr/X11R6/lib/* $tmpdir/usr/lib/
				cp  -R $nvrep/usr/X11R6/lib/modules/* $tmpdir/usr/lib/xorg/modules/
				cp  $nvrep/usr/lib/* $tmpdir/usr/lib/
			fi
			cd $tmpdir ; tar cvf $nvtar/$tmpdir-`uname -r`.tar.gz ./lib ./usr	
			cd $nvdir ; rm -R $nvtmp/*
			echo -e "\n$v  Your archive is created.\n  It known under the name$j $tmpdir-`uname -r`"
			sleep 1
		else
			echo -e "$y Houston ! We got a problem over there ! \n$v Is there a little bug in archive creation? "
			echo -e " Check the script or contact the author!"
			rm -f -R $nvtmp/* ; menu
		fi
	}

echo -e " $(drivercall)"
# analyse la version du pilote actuel avant extraction du pkg.run
	if [ -f $nvdl/nv-update-$(version_id) ] ; then
		pkg="`ls $nvdl | grep $(version_id)`" 
		echo -e "$y Installed driver version is$j $(version_id)."
		echo -e "$y The package version is$j ${pkg#*te-}.$y"
		if [[ -d $(drivercall) ]] ; then
			echo -e "$r The directory is already there,$y\n I don't proceed to extraction."
			sleep 1
		else
			echo -e "$r the directory doesn't eist,$y\n I proceed to extraction."
			sh $nvdl/$pkg -x
		fi
		nvrepls=`ls $nvtmp | grep 'NVIDIA'`
		nvrep=`echo ${nvrepls%/}`; 
		ls -v $nvtmp | grep '$(version_id)'
			if [[ -d $nvtmp/$nvrep ]] ; then
				echo -e "\f$r>>$y $nvrep ready ! $r <<$y\n" ; sleep 2
			else
				echo -e "\f$j $nvrep not here...check your package and/or script$y\n" ; sleep 1
				rm -f -R $nvtmp/* ; menu
			fi
		do_tar
		rm -f -R $nvtmp/* ; menu
		menu
	else
	    echo -e "$y The directory$j $(version_id)$y doesn't exist.$v\n"
	    echo -e "$v Back to menu.\n"
	    rm -f -R $nvtmp/* ; menu
	fi
}

### RESTART X SESSION
function dm_restart(){
	if [[ -e /tmp/.X0-lock ]] ; then
	    if [[ -e /var/run/gdm.pid ]] ; then
		gdm-stop ; gdm
	    else
		killall gdm-binary ; gdm
	    fi
	else
	    killall gdm-binary
	    modprobe -r nvidia ; depmod -qa
	    modprobe nvidia
	    gdm
	fi
}
### UNINSTALL fUnction
function uninstall(){
	echo -e "\n$r --->$y     UNINSTALL RUNING!$r     <---$y\r"
	sh $nvdir/nvidia-installer --uninstall
	echo -e "\f$v Done, bach to menu. \n" ; sleep 2
	menu
}
### START MENU
function menu(){
echo -e "$v\n Installation method:$j\r"
PS3="`echo -e "\n$v Your Choice$j ==>"` "
select choice in "$A" "$B" "$C" "$D" "$E" "$F" "$G" "$H" "$I"; do 
	
	case $choice in
		"$A")
			echo -e "$v--->  Installation from a local archive.  <---\r" ; from_archive
		;;
		"$B")
			echo -e "$v--->  Update from a local NVIDIA package.  <---\r" ; from_directory	
		;;
		"$C")
			echo -e "\n$r--->$v  Retriving informations from NVIDIA server.$r  <---$v\n" ; from_net
		;;
		"$D")
			echo -e "\n$r--->$v  Backup of an actual driver.$r <---\n$y" ; archive
		;;
		"$E")
			echo -e "$v--->  remove the present driver <---\r" ; uninstall
		;;
		"$F")
			echo -e "$v Stopping the Graphic Interface "
			sleep 2 ; /usr/sbin/gdm-stop ; exit 1
		;;
		"$G")
			echo -e "$v Restarting the Graphic Interface "
			sleep 2 ; dm_restart ; exit 0
		;;
		"$H")
			echo -e "$v\t Retrieving list ... \f"
			sleep 1 ; check_update; menu ; rm -f $nvtmp/drvlist $nvtmp/last_up 
		;;
		"$I")
			echo -e "$t\n" ; exit 0
		;;
	esac
done
}

### FIRST START
echo -e "\f\t$j          --- WELCOME TO THE NVIDIA \"BIDULEUR\" ---         \r"
echo -e "\t$j        ---    the propriatary driver stuffer    ---         \f" ; sleep 1
echo -e "\f$v  This script intend to help you to install, update, backup,\r"
echo -e "  your Nvidia drivers. It will help you to turn back to previous or \r"
echo -e "  prefered version in case of bug or whatever.\n"

## Creation of necessary directories in case they arent already there.:
distro
echo -e "$v  System :\t\t $j$DISTRO$v \n  Processor type :\t $j$ARCH$v \n  Board :\t\t$j$board \n"
echo -e "$v  Working directories control.\n"
if [ -d $nvdir ] ; then
	mkdir -p $nvtar $nvtmp $nvlog $nvupdate $nvdl
fi
chmod a+x $nvdl/*
## dep control
dep_control
presence=`ls -Cd $nvdir`
ls -Cd $nvdir > /dev/null
if [ $? -eq 0 ] ; then
	echo -e "$j  ${presence#*4/} MAKER$v directories are present" #; ls $nvdir
	xlocksession
	else
	if [ $? -ne 0 ] ; then 
		echo -e "$y\f  The$j NVIDIA MAKER$y directory doesn't exist\f"
		echo -e "$v  Choose the directory you want, I prefer /usr/local/src/"
		echo -e "$v  but it is really arbitrary:\n   $y==>$j \c"
		read -e sourcedir
		mkdir -p $sourcedir/NVIDIA
			if [ $? -eq 0 ] ; then
				sed -i "29cnvdir=\"$sourcedir/NVIDIA\"" /home/dvr/nvidia-maker-*.sh
			fi
	fi
fi
### SCRIPT INTRO
menu
exit 0
